﻿using HardcoreGame.Common.UI;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;

namespace HardcoreGame.Common.Systems
{
    public class FractureUISystem : ModSystem
    {
        internal FractureUIState FractureUI;
        private UserInterface _fractureInterface;

        public override void Load()
        {
            FractureUI = new FractureUIState();
            _fractureInterface = new UserInterface();
            _fractureInterface.SetState(FractureUI);
        }

        public override void UpdateUI(GameTime gameTime)
        {
            _fractureInterface?.Update(gameTime);
        }

        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int resourceBarIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Resource Bars"));
            if (resourceBarIndex != -1)
            {
                layers.Insert(resourceBarIndex + 1, new LegacyGameInterfaceLayer(
                    "HardcoreGame: Fracture UI",
                    delegate
                    {
                        _fractureInterface.Draw(Main.spriteBatch, new GameTime());
                        return true;
                    },
                    InterfaceScaleType.UI)
                );
            }
        }

        public void ToggleUI()
        {
            if (_fractureInterface.CurrentState == null)
            {
                _fractureInterface.SetState(FractureUI);
            }
            else
            {
                _fractureInterface.SetState(null);
            }
        }
    }
}